package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.wcs.service.enums.DeviceType;
import com.suray.wcs.service.system.entity.DeviceDB;
import com.suray.wcs.service.system.entity.PlcDB;
import com.suray.wcs.service.system.mapper.DeviceDBMapper;
import com.suray.wcs.service.system.service.IDevicedbService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @param
 * @author WX
 * @since 2019年11月11日
 */
@Service
public class DeviceDBServiceImpl extends ServiceImpl<DeviceDBMapper, DeviceDB> implements IDevicedbService {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DeviceDBMapper devicedbMapper;

    /**
     * 分页查询
     *
     * @param devicedb
     * @return
     * @throws Exception
     */
    @Override
    public List<DeviceDB> findDeviceList(DeviceDB devicedb, DeviceType plcDeviceType) {
        QueryWrapper<DeviceDB> queryWrapper = new QueryWrapper<DeviceDB>();
        if (devicedb.getType() != null) {
            queryWrapper.lambda().eq(DeviceDB::getType, plcDeviceType);
        }
        return devicedbMapper.selectList(queryWrapper);
    }

    /**
     * 插入数据库，
     *
     * @param deviceDB
     */
    @Override
    public void insertDevice(DeviceDB deviceDB) {
        if (deviceDB != null) {
            this.save(deviceDB);
            log.info("插入设备 " + deviceDB);
        }
    }

    /**
     * 根据类型查询设备
     *
     * @param devicedb
     * @return List<Devicedb>
     */
    @Override
    public List<DeviceDB> findByType(DeviceType devicedb) {
        List<DeviceDB> deviceDBS = this.list(new QueryWrapper<DeviceDB>().lambda().eq(DeviceDB::getType, devicedb));
        return deviceDBS;
    }

    /**
     * 更新设备
     */
    @Override
    public void updateDevice(DeviceDB devicedb) {
        if (devicedb.getId() != null) {
            devicedbMapper.updateById(devicedb);
            log.info("更新设备" + devicedb);
        }
    }

    @Override
    public int updateDisableById(DeviceDB devicedb) {
        return devicedbMapper.updateById(devicedb);
    }

    @Override
    public DeviceDB findByNode(String nodeStr) {
        DeviceDB deviceDB = this.getOne(new QueryWrapper<DeviceDB>().lambda().eq(DeviceDB::getLocation, nodeStr));
        return deviceDB;
    }


}
